﻿using System;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainCommitteeExpense : BasePage<MainCommitteeExpenseViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.storeProjects.DataSource = this.ViewModel.GetUserProjects();
            storeProjects.DataBind();
            double mainCommBalance = this.ViewModel.GetMainBranchBalance();
            txtMainCommitteeBalance.Text = mainCommBalance.ToString();
            if (mainCommBalance >= 1)
                txtExpenseAmount.MaxValue = mainCommBalance;
            else
                btnWithDraw.Disabled = true;
        }

        /// <summary>
        /// Handles the Changed event of the CmbProjects control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void CmbProjects_Changed(object sender, AjaxEventArgs e)
        {
            storeMainComitteeExpenses.DataSource = this.ViewModel.GetMainBranchExpenses(cmbProjects.SelectedItem.Value.ToGuid());
            storeMainComitteeExpenses.DataBind();
        }

        /// <summary>
        /// Handles the Clicked event of the btnWirhdraw control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnWirhdraw_Clicked(object sender, AjaxEventArgs e)
        {
            this.ViewModel.WithdrawFromMainCommittee(cmbProjects.SelectedItem.Value.ToGuid(), double.Parse(txtExpenseAmount.Text), Convert.ToDateTime(dateExpenseDate.Value));
            RefreshPageData();
        }

        /// <summary>
        /// Refreshes the page data.
        /// </summary>
        private void RefreshPageData()
        {
            storeMainComitteeExpenses.DataSource = this.ViewModel.GetMainBranchExpenses(cmbProjects.SelectedItem.Value.ToGuid());
            storeMainComitteeExpenses.DataBind();
            double mainCommBalance = this.ViewModel.GetMainBranchBalance();
            txtMainCommitteeBalance.Text = mainCommBalance.ToString();
            if (mainCommBalance >= 1)
                txtExpenseAmount.MaxValue = mainCommBalance;
            else
                btnWithDraw.Disabled = true;
        }

        public string PermissionName
        {
            get { return Permissions.MainCommitteeExpense.ToString(); }
        }
    }
}
